package com.laiketui.order.mapstruct;

import com.laiketui.order.api.dto.ConfirmOrderDetailDTO;
import com.laiketui.order.api.dto.OrderDTO;
import com.laiketui.order.api.dto.OrderDetailDTO;
import com.laiketui.order.domain.Order;
import com.laiketui.order.domain.OrderDetail;
import com.laiketui.order.entity.OrderDetailEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public interface OrderDetailMapstructMapper {
    OrderDetailMapstructMapper INSTANCE = Mappers.getMapper(OrderDetailMapstructMapper.class);


    @Mapping( target = "totalSupplierPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getTotalSupplierPrice()))")
    @Mapping( target = "supplierPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getSupplierPrice()))")
    @Mapping( target = "attribute",expression="java(com.laiketui.core.utils.help.DataSerializeHelp.getAttributeStr(orderDetail.getAttribute()))")
    ConfirmOrderDetailDTO toConfirmOrderDetailDTO(OrderDetail orderDetail);

    @Mapping( target = "totalSupplierPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getTotalSupplierPrice()))")
    @Mapping( target = "supplierPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getSupplierPrice()))")
    @Mapping( target = "attribute",expression="java(com.laiketui.core.utils.help.DataSerializeHelp.getAttributeStr(orderDetail.getAttribute()))")
    @Mapping( target = "productPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getProductPrice()))")
    @Mapping( target = "refundUserMoney",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getRefundUserMoney()))")
    @Mapping( target = "refundMoney",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getRefundMoney()))")
    @Mapping( target = "realMoney",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getRealMoney()))")
    @Mapping( target = "settlementPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getSettlementPrice()))")
    @Mapping( target = "costPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getCostPrice()))")
    @Mapping( target = "taxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getTaxation()))")
    @Mapping( target = "payPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getPayPrice()))")
    @Mapping( target = "totalSettlementPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getTotalSettlementPrice()))")
    OrderDetailDTO toDTO(OrderDetail orderDetail);

    @Mapping( target = "totalSupplierPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getTotalSupplierPrice()))")
    @Mapping( target = "supplierPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getSupplierPrice()))")
    @Mapping( target = "attribute",expression="java(com.laiketui.core.utils.help.DataSerializeHelp.getAttributeStr(orderDetail.getAttribute()))")
    @Mapping( target = "productPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getProductPrice()))")
    @Mapping( target = "refundUserMoney",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getRefundUserMoney()))")
    @Mapping( target = "refundMoney",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getRefundMoney()))")
    @Mapping( target = "realMoney",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getRealMoney()))")
    @Mapping( target = "settlementPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getSettlementPrice()))")
    @Mapping( target = "costPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getCostPrice()))")
    @Mapping( target = "taxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getTaxation()))")
    @Mapping( target = "payPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getPayPrice()))")
    @Mapping( target = "totalSettlementPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(orderDetail.getTotalSettlementPrice()))")
    OrderDetailEntity toEntity(OrderDetail orderDetail);


    default List<OrderDetailDTO> toDTOs(List<OrderDetail> orderParents){
        if(CollectionUtils.isEmpty(orderParents)){
            return new ArrayList<>();
        }
        return orderParents.stream().map(p ->{
            OrderDetailDTO convert = this.convert(p);
            return convert;
        }).collect(Collectors.toList());
    };

    default List<OrderDetailEntity> toEntitys(List<OrderDetail> orderParents){
        if(CollectionUtils.isEmpty(orderParents)){
            return new ArrayList<>();
        }
        return orderParents.stream().map(this::toEntity).collect(Collectors.toList());
    };

    default OrderDetailDTO convert(OrderDetail orderDetail){
        OrderDetailDTO orderDetailDTO = this.toDTO(orderDetail);
        String attribute = orderDetailDTO.getAttribute();
        String[] parameters = attribute.split(",");
        orderDetailDTO.setParameters(parameters);
        return orderDetailDTO;
    }
}
